local danji = fk.CreateSkill {

  name = "joy__danji",

  tags = { Skill.Wake, },

}



danji:addEffect(fk.DamageCaused, {
  name = "joy__danji",
  frequency = Skill.Wake,
  events = {fk.DamageCaused,fk.CardUsing, fk.CardResponding},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(danji.name) and player:usedSkillTimes(danji.name, Player.HistoryGame) == 0 then
      return true
    end
  end,
  can_wake = function(self, event, target, player, data)
    if event == fk.DamageCaused then
      return player:getMark("@joy__danjidamage") + data.damage > 6
    else
      return player:getMark("@joy__danjicard") + 1 > 5
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
      room:setPlayerMark(player,"@joy__danjicard",0)
      room:setPlayerMark(player,"@joy__danjidamage",0)
      if player:isWounded() then
        player.room:recover({
          who = player,
          num = player.maxHp - player.hp,
          recoverBy = player,
          skillName = danji.name
          })
      end
      room:handleAddLoseSkills(player, "joy__yuma|joy__nuzhan", nil)
  end,
})
danji:addEffect(fk.CardUsing, {
  name = "joy__danji",
  frequency = Skill.Wake,
  events = {fk.DamageCaused,fk.CardUsing, fk.CardResponding},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(danji.name) and player:usedSkillTimes(danji.name, Player.HistoryGame) == 0 then
      return true
    end
  end,
  can_wake = function(self, event, target, player, data)
    if event == fk.DamageCaused then
      return player:getMark("@joy__danjidamage") + data.damage > 6
    else
      return player:getMark("@joy__danjicard") + 1 > 5
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
      room:setPlayerMark(player,"@joy__danjicard",0)
      room:setPlayerMark(player,"@joy__danjidamage",0)
      if player:isWounded() then
        player.room:recover({
          who = player,
          num = player.maxHp - player.hp,
          recoverBy = player,
          skillName = danji.name
          })
      end
      room:handleAddLoseSkills(player, "joy__yuma|joy__nuzhan", nil)
  end,
})
danji:addEffect(fk.CardResponding, {
  name = "joy__danji",
  frequency = Skill.Wake,
  events = {fk.DamageCaused,fk.CardUsing, fk.CardResponding},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(danji.name) and player:usedSkillTimes(danji.name, Player.HistoryGame) == 0 then
      return true
    end
  end,
  can_wake = function(self, event, target, player, data)
    if event == fk.DamageCaused then
      return player:getMark("@joy__danjidamage") + data.damage > 6
    else
      return player:getMark("@joy__danjicard") + 1 > 5
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
      room:setPlayerMark(player,"@joy__danjicard",0)
      room:setPlayerMark(player,"@joy__danjidamage",0)
      if player:isWounded() then
        player.room:recover({
          who = player,
          num = player.maxHp - player.hp,
          recoverBy = player,
          skillName = danji.name
          })
      end
      room:handleAddLoseSkills(player, "joy__yuma|joy__nuzhan", nil)
  end,
})

danji:addEffect(fk.DamageCaused, {
  name = "#joy__danji_trigger",
  mute = true,
  events = {fk.DamageCaused,fk.CardUsing, fk.CardResponding},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(danji.name) and player:usedSkillTimes("joy__danji", Player.HistoryGame) == 0 then
      return true
    end
  end,
  on_cost =Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
      if event == fk.CardResponding or event == fk.CardUsing then
          room:addPlayerMark(player,"@joy__danjicard",1)
      elseif event == fk.DamageCaused then
        room:addPlayerMark(player,"@joy__danjidamage",data.damage)
      end
      player:setSkillUseHistory("joy__danji", 0, Player.HistoryGame)
  end,
})
danji:addEffect(fk.CardUsing, {
  name = "#joy__danji_trigger",
  mute = true,
  events = {fk.DamageCaused,fk.CardUsing, fk.CardResponding},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(danji.name) and player:usedSkillTimes("joy__danji", Player.HistoryGame) == 0 then
      return true
    end
  end,
  on_cost =Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
      if event == fk.CardResponding or event == fk.CardUsing then
          room:addPlayerMark(player,"@joy__danjicard",1)
      elseif event == fk.DamageCaused then
        room:addPlayerMark(player,"@joy__danjidamage",data.damage)
      end
      player:setSkillUseHistory("joy__danji", 0, Player.HistoryGame)
  end,
})
danji:addEffect(fk.CardResponding, {
  name = "#joy__danji_trigger",
  mute = true,
  events = {fk.DamageCaused,fk.CardUsing, fk.CardResponding},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(danji.name) and player:usedSkillTimes("joy__danji", Player.HistoryGame) == 0 then
      return true
    end
  end,
  on_cost =Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
      if event == fk.CardResponding or event == fk.CardUsing then
          room:addPlayerMark(player,"@joy__danjicard",1)
      elseif event == fk.DamageCaused then
        room:addPlayerMark(player,"@joy__danjidamage",data.damage)
      end
      player:setSkillUseHistory("joy__danji", 0, Player.HistoryGame)
  end,
})

return danji